package com.qire.manhua.dal.service;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.viewModel.impl.RecommendMoreViewModel;
import com.qire.manhua.model.vo.RecommendModuleMoreItemVO;
import com.qire.manhua.view.style.RadiusBackgroundSpan;

import java.util.List;

public class RecommendMoreService extends DataService {

    private final IPageDataDAL pageDataDAL = WarehouseFactory.create(this, IPageDataDAL.class);

    private final RecommendMoreViewModel viewModel;

    public RecommendMoreService(RecommendMoreViewModel viewModel) {
        this.viewModel = viewModel;
    }

    public void loadPageData() {
        pageDataDAL.fetchRecommendModuleMoreItemData(viewModel.moduleId(), viewModel.pageNo());
    }

    @StrategyAnnotation(event = Url.v5_moreRecommend)
    public void fetchRecommendModuleMoreItemDataCallback(List<RecommendModuleMoreItemVO> dataVOList, boolean isCache) {

        viewModel.refreshLayoutFinish(300);

        if(ListUtil.isEmpty(dataVOList)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        for(RecommendModuleMoreItemVO vo : dataVOList) {

            SpannableStringBuilder builder = new SpannableStringBuilder();
            if(vo.tags != null) {
                for (String label : vo.tags) {
                    int startIndex = builder.length();
                    builder.append(label + " ");
                    RadiusBackgroundSpan bgSpan = new RadiusBackgroundSpan(Color.rgb(0x99, 0x99, 0x99), 6);//25);
                    builder.setSpan(bgSpan, startIndex, startIndex + label.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                }
            }

            int comicId = vo.book_id;
            String coverImg = vo.book_unruly;
            String floatTips = vo.end_state == 0 ? "更新至" + vo.chapter_px + "话" : "全" + vo.chapter_px + "话";
            String title = vo.book_name;
            String author = "作者：" + vo.book_author;
            String comicBrief = vo.title;
            CharSequence label = builder;
            String popularityValue = "人气值: " + StringUtil.toSymbol(vo.read_cnt);

            viewModel.addMoreItem(comicId, coverImg, floatTips, title, author, comicBrief, label, popularityValue);
        }

        viewModel.nextPage();
        viewModel.refreshModuleAdapter();

    }

} 
